package com.seer.seertest.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

@Slf4j
@Component
public class JwtUtils {

    private static String secretKeyStatic;
    @Value("${security.jwt.secret-key}")
    private String secretKey;
    @PostConstruct
    public void init() {
        secretKeyStatic = secretKey; // 同步到静态变量
    }

    private static final long EXPIRATION_TIME = 3600000; // 1小时

    public static String generateToken(String username) {
        return Jwts.builder()
                .setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .signWith(Keys.hmacShaKeyFor(secretKeyStatic.getBytes())) // 使用密钥
                .compact();
    }
}